package thread;

import com.ms.common.utils.ThreadUtils;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

/**
 * @author ganbo
 * @date 2022/10/29 17:49
 */

@Slf4j
public class ThreadPoolTest {


    @Test
    public void invokeAllTest() throws InterruptedException, ExecutionException {
        ExecutorService executorService = Executors.newFixedThreadPool(2);

        List<Future<String>> resultList = executorService.invokeAll(Arrays.asList(
                () -> {
                    log.debug("begin");
                    ThreadUtils.sleepSeconds(1);
                    return "1";
                }
                ,
                () -> {
                    log.debug("begin");
                    ThreadUtils.sleepSeconds(2);
                    return "2";
                }
                ,
                () -> {
                    log.debug("begin");
                    ThreadUtils.sleepSeconds(3);
                    return "3";
                }
        ));

        for (Future<String> stringFuture : resultList) {
            System.out.println(stringFuture.get());
        }

    }
}
